package com.cool.modules.finance.service.account.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.cool.core.base.BaseServiceImpl;
import com.cool.core.exception.CoolException;
import com.cool.modules.finance.entity.account.FinanceAccountEntity;
import com.cool.modules.finance.entity.account.FinanceTrialBalanceEntity;
import com.cool.modules.finance.entity.coin.FinanceCoinEntity;
import com.cool.modules.finance.mapper.account.FinanceTrialBalanceMapper;
import com.cool.modules.finance.service.account.FinanceAccountService;
import com.cool.modules.finance.service.account.FinanceTrialBalanceService;
import com.cool.modules.finance.service.coin.FinanceCoinService;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;

/**
 * 试算表
 * @author xingzhaojun
 */
@Service
@Transactional(rollbackFor = Exception.class)
@RequiredArgsConstructor
public class FinanceTrialBalanceServiceImpl extends BaseServiceImpl<FinanceTrialBalanceMapper, FinanceTrialBalanceEntity> implements FinanceTrialBalanceService {

    final FinanceAccountService accountService;

    final FinanceCoinService coinService;

    @Override
    public Long add(FinanceTrialBalanceEntity entity) {
        FinanceAccountEntity account = accountService.getById(entity.getAccountId());
        if (null==entity.getCapital()){
            entity.setCapital(account.getAccountBalance());
        }
        entity.setInterest(entity.getTransferMoney().subtract(entity.getCapital()));
        entity.setRedeemDate(entity.getTransferTime()==null?new Date():entity.getTransferTime());
        entity.setTrialDate(new Date());
        entity.setDays(DateUtil.between(account.getValueDate(),entity.getRedeemDate(),DateUnit.DAY)+1);
        //设置中位年化利率
        entity.setMedianRate(account.getStartRate().equals(account.getEndRate())?account.getStartRate():account.getEndRate().subtract(account.getStartRate()).abs().divide(new BigDecimal(2).setScale(2, RoundingMode.HALF_UP)).add(account.getStartRate().min(account.getEndRate())));

        entity.setCoinId(account.getCoinId());
        FinanceCoinEntity coin = coinService.getById(account.getCoinId());
        if (coin != null){
            entity.setCoinName(coin.getCoinName());
            entity.setCoinSymbol(coin.getSymbol());
        }
        if (entity.getDays() > 0) {
            BigDecimal annualRate = entity.getInterest().divide(entity.getCapital(), 8, RoundingMode.HALF_UP)
                    .multiply(new BigDecimal(365))
                    .divide(BigDecimal.valueOf(entity.getDays()), 8, RoundingMode.HALF_UP).
                    multiply(new BigDecimal(100)).setScale(2,RoundingMode.HALF_UP);
            entity.setRate(annualRate);
        }
        //是否存在相同的记录
        if (exists(new QueryWrapper().
                eq(FinanceTrialBalanceEntity::getAccountId,entity.getAccountId()).
                eq(FinanceTrialBalanceEntity::getDays,entity.getDays()).
                eq(FinanceTrialBalanceEntity::getRate,entity.getRate()).
                eq(FinanceTrialBalanceEntity::getMedianRate,entity.getMedianRate()))){
            throw new CoolException("该记录已存在");
        }
        return super.add(entity);
    }
}
